'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: helper.py
* @Time: 2025/9/22
* @All Rights Reserve By Brtc
'''
import importlib
from datetime import datetime
from hashlib import sha3_256
from typing import Any

from langchain_core.documents import Document as LCDocument


def dynamic_import(module_name:str, symbol_name:str)->Any:
    """动态导入特定模块下的， 特定函数"""
    module = importlib.import_module(module_name)
    return getattr(module, symbol_name)


def add_attribute(attr_name:str, attr_value:Any):
    """装饰器函数,为特定的函数添加相应的属性,第一个参数为属性名字,第二个参数为属性值"""
    def decorator(func):
        setattr(func, attr_name, attr_value)
        return func
    return decorator


def generate_text_hash(text:str)->str:
    """将传递进来的文本计算对应的 hash值"""
    # 如果 text 为None 也能生成hash 值
    text = str(text) + "None"
    return sha3_256(text.encode()).hexdigest()


def datetime_to_timestamp(dt:datetime):
    """将传入的datatime时间转换成时间戳，如果数据不存在则返回0"""
    if dt is None:
        return 0
    return int(dt.timestamp())


def combine_documents(documents:list[LCDocument])->str:
    """将对应的文档列表使用换行符进行合并"""
    return "\n\n".join([document.page_content for document in documents])